#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include<conio.h>

#define _CRT_SECURE_NO_WARNINGS

struct Student
{
	char name[20];
	int age;
	int num;
	char gender[20];
};
struct Node
{
	struct Student data;
	struct Node* next;
};


struct Node* createHead()
{
	struct Node* headNode = (struct Node*)malloc(sizeof(struct Node));
	headNode->next = NULL;
	return headNode;
}
struct Node* createNode(struct Student  data)
{
	struct Node * newNode = (struct Node*)malloc(sizeof(struct Node));
	newNode->data = data;
	newNode->next = NULL;
	return newNode;
}
void insertByHead(struct Node* headNode, struct Student data)
{
	struct Node* newNode = createNode(data);
	newNode->next = headNode->next;
	headNode->next = newNode;
}

void searchAllInfo(struct Node* headNode, char *name)
{
	struct Node* pMove = headNode->next;
	while (pMove != NULL)
	{

		if (!strcmp(pMove->data.name, name))
			printf("%s\t%d\t%d\t%s\n", pMove->data.name, pMove->data.age, pMove->data.num, pMove->data.gender);
		pMove = pMove->next;
	}
}

//按姓名查找
struct Node* searchInfo(struct Node* headNode, char *name)
{
	struct Node* pMove = headNode->next;
	while (pMove != NULL&&strcmp(pMove->data.name, name))
	{
		pMove = pMove->next;
	}
	return pMove;
}


void deleteByAppoin(struct Node* headNode, char *name)
{
	struct Node* posNodeLeft = headNode;
	struct Node* posNode = headNode->next;
	while (posNode != NULL&&strcmp(posNode->data.name ,name))
	{
		posNodeLeft = posNode;
		posNode = posNodeLeft->next;
	}
	if (posNode == NULL)
	{
		printf("未找到指定位置,无法删除!\n");
	}
	else
	{
		posNodeLeft->next = posNode->next;
		free(posNode);
		posNode = NULL;
		printf("删除成功!\n");
	}
}


void deleteAll(struct Node* headNode, char *name)
{
	while (searchInfo(headNode, name) != NULL)
	{
		deleteByAppoin(headNode, name);
	}
}

void modifyALL(struct Node* headNode, char *name,struct Student newInfo)
{
	while (searchInfo(headNode, name) != NULL)
	{
		searchInfo(headNode, name)->data = newInfo;
	}
}

void printList(struct Node* headNode)
{
	struct Node* pMove = headNode->next;

	printf("name\tage\tnum\tgender\n");
	while (pMove != NULL)
	{

		printf("%s\t%d\t%d\t%s\n", pMove->data.name, pMove->data.age, pMove->data.num, pMove->data.gender);
		pMove = pMove->next;
	}
}


void BubbleSortList(struct Node*headNode)
{

	for (struct Node* p = headNode->next; p != NULL; p = p->next)
	{
		for (struct Node* q = headNode->next; q->next != NULL; q = q->next)
		{
			if ((q->data.age > q->next->data.age))
			{
				struct Student tempData = q->data;
				q->data = q->next->data;
				q->next->data = tempData;
			}
		}
	}
	printList(headNode);
}

void BubbleSortByName(struct Node*headNode)
{

	for (struct Node* p = headNode->next; p != NULL; p = p->next)
	{
		for (struct Node* q = headNode->next; q->next != NULL; q = q->next)
		{
			if (strcmp(q->data.name, q->next->data.name)>0)
			{
				struct Student tempData = q->data;
				q->data = q->next->data;
				q->next->data = tempData;
			}
		}
	}
	printList(headNode);
}

struct Node* list = NULL;

//菜单
void makeMenu()
{
	printf("-----------【学生管理系统】--------\n");
	printf("\t0.退出系统\n");
	printf("\t1.录入信息\n");
	printf("\t2.查看信息\n");
	printf("\t3.修改信息\n");
	printf("\t4.查找信息\n");
	printf("\t5.删除信息\n");
	printf("\t6.排序显示\n");
	printf("-------------------------------------\n");
}

void keyDown()
{
	int userKey = 0;
	struct Student tempData;	//存储用户的数据
	struct Node* posNode = NULL;
	scanf("%d", &userKey);
	switch (userKey)
	{
	case 0:
		printf("正常退出，欢迎下次光临!\n");
        //system("pause");
        printf("请按任意键继续\n");
        getch();
		exit(0);
		break;
	case 1:

		printf("请输入信息:(姓名,年龄,学号,性别):");
		scanf("%s%d%d%s", tempData.name, &tempData.age, &tempData.num, tempData.gender);
		insertByHead(list,tempData);
		break;
	case 2:
		printList(list);
		break;
	case 3: //修改 --->修改作业
		printf("请输入要修改的姓名:");
		scanf("%s", tempData.name);

		posNode = searchInfo(list, tempData.name);
		if (posNode == NULL)
		{
			printf("未找到指定位置，无法修改！");
		}
		else
		{
			printf("请输入新的信息:(姓名,年龄,学号,性别):");
			scanf("%s%d%d%s", posNode->data.name, &posNode->data.age, &posNode->data.num, posNode->data.gender);
			//posNode->data=tempInfo;
			printf("修改成功!");
		}
		break;
	case 4: //查找
		printf("请输入要查找的姓名:");
		scanf("%s", tempData.name);
		searchAllInfo(list, tempData.name);
		break;
	case 5: //删除
		printf("请输入要删除的姓名:");
		scanf("%s", tempData.name);
		deleteByAppoin(list, tempData.name);
		break;
	case 6:
		BubbleSortList(list);
		break;
	default:
		printf("输入错误!,重新输入!\n");
		break;
	}
}
int main()
{
	list = createHead();
	while (1)
	{
		makeMenu();
		keyDown();
        //system("pause");
        printf("请按任意键继续\n");
        getch();
		system("cls");
	}
	//system("pause");
    printf("请按任意键继续\n");
    getch();
	return 0;
}
